<template>
  <div class="goods-item"  @click="itemClick">
<!--    vue组件提供了一个load方法可以用于监听图片是否加载完成-->
    <img v-lazy="showImage" alt="" @load="itemImageLoad">
    <div class="goods-info">
      <p>{{ goodsItem.title }}</p>
      <span class="price">{{ goodsItem.price }}</span>
      <span class="collect">{{ goodsItem.cfav }}</span>
    </div>
  </div>
</template>

<script>
  export default {
    name: "GoodsListItem",
    data() {
      return {
      }
    },
    props: {
      goodsItem: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    computed: {
      showImage() {
        return this.goodsItem.img || this.goodsItem.image || this.goodsItem.show.img
      }
    },
    methods: {
      itemImageLoad() {
        //法一  ：由于此处在首页和详情页均需要知道图片是否加载完成  所以应注意发送图片加载完成的数据给的页面
        // if (this.$route.path.indexOf(`/home`)) {
        //   this.$bus.$emit('itemImageLoad')
        // } else if(this.$route.path.indexOf('/detail')) {
        //   this.$bus.$emit('detailImageLoad')
        // }

      //  法二
        this.$bus.$emit('itemImageLoad')
      },
      itemClick() {
        this.$router.push('/detail/' + this.goodsItem.iid)
      }
    }
  }
</script>

<style scoped>
  .goods-item {
    position: relative;

    padding-bottom: 40px;
  }
  .goods-item img {
    width: 100%;
    border-radius: 5px;
  }

  .goods-info {
    position: absolute;
    overflow: hidden;

    left: 0;
    right: 0;
    bottom: 5px;

    text-align: center;
    font-size: 12px;
  }



  .goods-info p {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    margin-bottom: 3px;
  }
  .goods-info .price {
    color: var(--color-high-text);
    margin-right: 20px;
  }

  .goods-info .collect {
    position: relative;
  }

  .goods-info .collect::before {
    content: '';
    position: absolute;

    left: -15px;
    top: -1px;
    width: 14px;
    height: 14px;

    background: url("~assets/img/common/collect.svg") 0 0/14px 14px;
  }
</style>